import 'package:flutter/material.dart';
import '../widgets/playlist_view.dart';
import 'package:provider/provider.dart';
import '../providers/song_provider.dart';
import '../widgets/player_sidebar.dart';
import '../widgets/search_view.dart';
import '../services/database_service.dart';
import '../models/song.dart';
import '../services/audio_player_service.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  late Future<void> _initFuture;
  final audioPlayerService = AudioPlayerService();
  Song? _currentSong;

  @override
  void initState() {
    super.initState();
    _initFuture = _initializeApp();
  }

  @override
  void dispose() {
    audioPlayerService.player.dispose();
    super.dispose();
  }

  Future<void> _initializeApp() async {
    final database = await DatabaseService().database;
    await Provider.of<SongProvider>(context, listen: false)
        .initialize(database);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 60,
        title: Consumer<SongProvider>(
          builder: (context, songProvider, child) {
            if (songProvider.isUpdating) {
              return Row(
                children: [
                  const Text('音乐播放器'),
                  const SizedBox(width: 16),
                  const SizedBox(
                    width: 16,
                    height: 16,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                      valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                    ),
                  ),
                  const SizedBox(width: 8),
                  Text(
                    '正在更新歌曲列表...',
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.white.withOpacity(0.8),
                    ),
                  ),
                ],
              );
            }
            //return const Text("ddd");
            return const SearchView();
          },
        ),
      ),
      body: FutureBuilder(
        future: _initFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          }
          if (snapshot.hasError) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text('初始化失败'),
                  Text('错误信息: ${snapshot.error}'),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        _initFuture = _initializeApp();
                      });
                    },
                    child: const Text('重试'),
                  ),
                ],
              ),
            );
          }
          return Row(
            children: [
              PlayerSidebar(
                currentSong: _currentSong,
                audioPlayerService: audioPlayerService,
              ),
              Expanded(
                child: Column(
                  children: [
                    Expanded(
                      child: PlaylistView(
                        onSongSelected: (song) {
                          setState(() {
                            _currentSong = song;
                          });
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          );
        },
      ),
    );
  }
} 